import request from './index'

/**
 * 获取收藏列表
 * @returns {Promise} 返回收藏列表
 */
export const getFavoriteList = () => {
  return request({
    url: '/favorite/list',
    method: 'get'
  })
}

/**
 * 添加收藏
 * @param {number} productId - 商品ID
 * @returns {Promise} 返回操作结果
 */
export const addFavorite = (productId) => {
  return request({
    url: '/favorite/add',
    method: 'post',
    params: { ProductId: parseInt(productId) }
  })
}

/**
 * 取消收藏
 * @param {number} productId - 商品ID
 * @returns {Promise} 返回操作结果
 */
export const deleteFavorite = (productId) => {
  return request({
    url: '/favorite/delete',
    method: 'delete',
    params: { ProductId: parseInt(productId) }
  })
}

/**
 * 判断商品是否已收藏
 * @param {number} productId - 商品ID
 * @returns {Promise<boolean>} 返回是否已收藏
 */
export const isFavorite = (productId) => {
  return request({
    url: '/favorite/isFavorite',
    method: 'get',
    params: { ProductId: parseInt(productId) },
    transformResponse: [data => {
      // 将响应数据转换为布尔值
      if (typeof data === 'string') {
        const parsedData = JSON.parse(data)
        return parsedData === true || parsedData === 'true'
      }
      return data === true || data === 'true'
    }]
  })
} 